package com.config;

import com.bean.anno.TableExcel;
import com.bean.other.TableBack;
import lombok.Data;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;

import java.util.*;

//步骤
@Component
@Data
public class Commons {

    private List<TableBack> tableBackList =new ArrayList<TableBack>();

    public Commons() throws ClassNotFoundException {
        scanForAnnotatedClasses();
    }

    //启动就会被调用
    public void scanForAnnotatedClasses() throws ClassNotFoundException {

        ClassPathScanningCandidateComponentProvider provider =
                new ClassPathScanningCandidateComponentProvider(false);
        // 过滤器：扫描实现了某个接口的类
        provider.addIncludeFilter(new AssignableTypeFilter(TableExcel.class));

        // 创建扫描器
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        // 设置过滤器，扫描指定注解的类
        TypeFilter filter = new AnnotationTypeFilter(TableExcel.class);
        scanner.addIncludeFilter(filter);

        // 扫描指定包
        Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents("com.bean");


        // 输出扫描到的类
        for (BeanDefinition beanDefinition : beanDefinitions) {


           /* System.out.println(beanDefinition.toString());
            System.out.println("Found annotated class: " + beanDefinition.getBeanClassName());*/
            Class cla = Class.forName(beanDefinition.getBeanClassName());
            TableExcel t = (TableExcel) cla.getAnnotation(TableExcel.class);
            /*System.out.println(t.content());*/

            String className = beanDefinition.getBeanClassName();
            String name = t.name();
            String type = t.type();
            String content = t.content();
            String jpaBeanName = t.jpaBeanName();


            TableBack tableBack = new TableBack();
            tableBack.setClassName(className);
            tableBack.setName(name);
            tableBack.setContent(content);
            tableBack.setJpaBeanName(jpaBeanName);

            tableBackList.add(tableBack);

            //System.out.println(beanDefinition.get);

        }
    }
}
